import ROSLIB from "roslib";

/**
 * 获取模式值
 * @param {ROSLIB.Ros} roslink Ros链接对象
 * @param {function} callback 回调函数
 * @param {Object} self 回调函数的参数，可传入组件环境指针this
 */
function getmode(roslink = null, callback, self) {
  var m = "";
  var getModeClient = new ROSLIB.Service({
    ros: roslink,
    name: "/add_two_ints",
    serviceType: "rospy_tutorials/AddTwoInts",
  });

  var request = new ROSLIB.ServiceRequest({
    name: "find",
    a: 1,
    p: "s"
  });

  getModeClient.callService(request, function (result) {

    if (callback != null && typeof callback === "function") {
      callback(self, result.feedback);
    } else {
      console.log("Function getmode: try callback failed. ");
    };

    console.log(
      "Result for service call on " +
      getModeClient.name +
      ": " +
      result.feedback
    );
    console.log(result.feedback);
    m = result.feedback;
    return m;
  });
}

/**
 * 向目标链接发送设置模式值
 * @param {ROSLIB.Ros} roslink Ros链接对象
 * @param {String} modeValue 设置模式的值
 * @param {function} callback 回调函数
 * @param {Object} self 回调函数的参数，可传入组件环境指针this
 */
function setmode(roslink = null, modeValue, callback, self) {
  var setModeClient = new ROSLIB.Service({
    ros: roslink,
    name: "/add_two_ints",
    serviceType: "rospy_tutorials/AddTwoInts",
  });

  // console.log("set mode by currentMode:" + newMode);
  var request = new ROSLIB.ServiceRequest({
    name: "set",
    a: modeValue,
    // a: 1,
    p: "s",
  });

  var fb = "";
  setModeClient.callService(request, function (result) {
    if (callback != null && typeof callback === "function") {
      callback(self);
    } else {
      console.log("Function setmode: try callback failed. ");
    };
    console.log(
      "Result for service call on " +
      setModeClient.name +
      ": " +
      result.feedback
    );
    fb = result.feedback;
    return fb;
  });
}

export { getmode, setmode };
